//
//  LFButton.m
//  AntFarms
//
//  Created by LF on 2017/3/3.
//  Copyright © 2017年 GG. All rights reserved.
//

#import "LFButton.h"

@implementation LFButton

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    _lf_iconImgView = [[UIImageView alloc]init];
    _lf_titleLb = [[UILabel alloc]init];
    _lf_titleLb.font = [UIFont systemFontOfSize:kNumFrom375(16)];
    _lf_titleLb.textColor = [UIColor lf_colorWithHexString:@"#636363"];
    
    [self addSubview:_lf_iconImgView];
    [self addSubview:_lf_titleLb];
}

- (void)setLf_title_string:(NSString *)lf_title_string
{
    _lf_title_string = lf_title_string;
    
    self.lf_titleLb.text = lf_title_string;
}

- (void)setLf_iconImage_name:(NSString *)lf_iconImage_name
{
    _lf_iconImage_name = lf_iconImage_name;
    
    self.lf_iconImgView.image = [UIImage imageNamed:lf_iconImage_name];
}


- (void)beginDrawIt
{
//    NSInteger textCount = self.lf_titleLb.text.length;
    
//    CGFloat titleFont = self.titleFont ? kNumFrom375(self.titleFont) : kNumFrom375(16);
    CGFloat iconWH = self.iconWH ? kNumFrom375(self.iconWH) : kNumFrom375(22);
    CGFloat margin = self.margin ? kNumFrom375(self.margin) : kNumFrom375(8);
//    CGFloat text_Length = textCount * (titleFont + kNumFrom375(2));
//    CGSize titleSize = [self.lf_titleLb.text sizeWithFont:self.lf_titleLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 30)];
     CGFloat text_Length = [self.lf_titleLb.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.lf_titleLb.font} context:nil].size.width+kNumFrom375(2);
    CGFloat whole_length = iconWH + margin + text_Length;
    
    
    [_lf_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.mas_centerX).with.offset(whole_length * 0.5);
    }];
    
    [_lf_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_lf_titleLb.mas_left).with.offset(-margin);
        make.centerY.equalTo(self);
        if (self.iconW) {
            make.height.equalTo(@(iconWH));
            make.width.equalTo(@(self.iconW));
        }else {
            make.width.height.equalTo(@(iconWH));
        }
        
    }];
}

#pragma mark - setter
//- (void)setSelected:(BOOL)selected {
//    [super setSelected:selected];
//    if (selected) {
//        [self popOutsideWithDuration: 0.5];
//        [self.explodeAnimationView animate];
//    } else {
//        [self popInsideWithDuration: 0.5];
//    }
//}


#pragma mark - Animate
- (void) beSelected:(BOOL) selected {
    if (selected) {
        [self popOutsideWithDuration: 0.5];
    } else {
        [self popInsideWithDuration: 0.5];
    }
}

- (void) popOutsideWithDuration: (NSTimeInterval) duringTime {
    __weak typeof(self) weakSelf = self;
    self.transform = CGAffineTransformIdentity;

    [UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime: 0
                                relativeDuration: 1 / 3.0
                                      animations: ^{
                                          typeof(self) strongSelf = weakSelf;
                                          strongSelf.transform = CGAffineTransformMakeScale(1.5, 1.5);
                                      }];
        [UIView addKeyframeWithRelativeStartTime: 1 / 3.0
                                relativeDuration: 1 / 3.0
                                      animations: ^{
                                          typeof(self) strongSelf = weakSelf;
                                          strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
                                      }];
        [UIView addKeyframeWithRelativeStartTime: 2 / 3.0
                                relativeDuration: 1 / 3.0
                                      animations: ^{
                                          typeof(self) strongSelf = weakSelf;
                                          strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
                                      }];
    } completion:nil];
}

- (void) popInsideWithDuration: (NSTimeInterval) duringTime {
    __weak typeof(self) weakSelf = self;
    self.transform = CGAffineTransformIdentity;
    
    [UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime: 0
                                relativeDuration: 1 / 2.0
                                      animations: ^{
                                          typeof(self) strongSelf = weakSelf;
                                          strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
                                      }];
        [UIView addKeyframeWithRelativeStartTime: 1 / 2.0
                                relativeDuration: 1 / 2.0
                                      animations: ^{
                                          typeof(self) strongSelf = weakSelf;
                                          strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
                                      }];
    } completion:nil];
    
}

@end


















/*
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_lf_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.centerX.equalTo(self);
        make.width.height.equalTo(@(kNumFrom375(46)));
    }];
    
    [_lf_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_lf_iconImgView.mas_bottom).with.offset(kNumFrom375(4));
        make.centerX.equalTo(_lf_iconImgView);
        make.height.equalTo(@(kNumFrom375(22)));
    }];
}
*/

